#include "VertexDeclarationDX10.h"
#include "Engine/AuxTypesDX10.h"

CVertexDeclarationDX10::CVertexDeclarationDX10( )
{
    m_pInputLayout = NULL;
}

CVertexDeclarationDX10::~CVertexDeclarationDX10( )
{
    SAFE_RELEASE(m_pInputLayout);
}

mVoid CVertexDeclarationDX10::Setup( )
{
    g_D3DDevice.pD3D10Device->IASetInputLayout(m_pInputLayout);
}

mBool CVertexDeclarationDX10::Create( const void *a_pShaderByteCode, size_t a_uByteCodeLength )
{
    if (m_pInputLayout)
        return false;

    HRESULT hr;
    D3D10_INPUT_ELEMENT_DESC *pInputLayoutDesc;
    pInputLayoutDesc = new D3D10_INPUT_ELEMENT_DESC[m_Declaration.size()];
    size_t i;
    size_t uOffset = 0;
    for (i = 0; i < m_Declaration.size(); i++)
    {
        pInputLayoutDesc[i].SemanticName         = m_Declaration[i].sSemanticName.c_str();
        pInputLayoutDesc[i].SemanticIndex        = m_Declaration[i].uSemanticIndex;
        pInputLayoutDesc[i].Format               = DX10VarType(m_Declaration[i].VarType);
        pInputLayoutDesc[i].InputSlot            = m_Declaration[i].uInputSlot;
        pInputLayoutDesc[i].AlignedByteOffset    = uOffset;
        pInputLayoutDesc[i].InputSlotClass       = D3D10_INPUT_PER_VERTEX_DATA;
        pInputLayoutDesc[i].InstanceDataStepRate = 0;
        uOffset += g_uVarTypeSize[m_Declaration[i].VarType];
    }

    hr = g_D3DDevice.pD3D10Device->CreateInputLayout(pInputLayoutDesc, m_Declaration.size(), 
        a_pShaderByteCode, a_uByteCodeLength, 
        &m_pInputLayout);

    delete [] pInputLayoutDesc;
    return (hr == S_OK);
}

